﻿@namespace BlazorSample

@implements IDisposable

<button @ref="_buttonElement"
        @onclick="HandleClick"
        @onpointerdown="StartPress"
        @onpointerup="EndPress"
        @onpointerleave="EndPress"
        @onpointercancel="EndPress"
        disabled="@Disabled"
        class="@($"long-press-button {Class} {(Disabled ? "disabled" : "")}")"
        @attributes="AdditionalAttributes">
    @ChildContent
</button>

@code {
    private ElementReference _buttonElement;
    private System.Threading.Timer _longPressTimer;
    private bool _isLongPress;
    private bool _isPointerDown;
    private DateTime _pressStartTime;

    [Parameter]
    public RenderFragment ChildContent { get; set; }

    [Parameter]
    public EventCallback OnClick { get; set; }

    [Parameter]
    public EventCallback OnLongPressStart { get; set; }

    [Parameter]
    public EventCallback OnLongPressContinued { get; set; }

    [Parameter]
    public EventCallback OnLongPressEnd { get; set; }

    [Parameter]
    public int LongPressDuration { get; set; } = 500; // 长按触发初始延迟(毫秒)

    [Parameter]
    public int ContinuedPressInterval { get; set; } = 500; // 持续触发间隔(毫秒)

    [Parameter]
    public bool Disabled { get; set; }

    [Parameter(CaptureUnmatchedValues = true)]
    public Dictionary<string, object> AdditionalAttributes { get; set; }

    [Parameter]
    public string Class { get; set; }

    protected override void OnParametersSet()
    {
        // 当按钮被禁用时，立即停止所有正在进行的按压操作
        if (Disabled && _isPointerDown)
        {
            EndPress();
        }
    }

    private async Task HandleClick()
    {
        if (Disabled) return;

        // 如果是长按事件，则不触发点击事件
        if (!_isLongPress && OnClick.HasDelegate)
        {
            await OnClick.InvokeAsync();
        }

        _isLongPress = false;
    }

    private void StartPress()
    {
        if (Disabled) return;

        _isPointerDown = true;
        _isLongPress = false;
        _pressStartTime = DateTime.Now;

        // 设置长按计时器
        _longPressTimer = new System.Threading.Timer(LongPressCallback, null, LongPressDuration, Timeout.Infinite);
    }

    private async void LongPressCallback(object state)
    {
        if (!_isPointerDown || Disabled) return;

        _isLongPress = true;

        await InvokeAsync(async () =>
        {
            // 触发长按开始事件
            if (OnLongPressStart.HasDelegate)
            {
                await OnLongPressStart.InvokeAsync();
            }

            // 重置计时器为持续触发模式
            _longPressTimer?.Dispose();
            _longPressTimer = new System.Threading.Timer(ContinuedPressCallback, null, ContinuedPressInterval, ContinuedPressInterval);
        });
    }

    private async void ContinuedPressCallback(object state)
    {
        if (!_isPointerDown || Disabled) return;

        await InvokeAsync(async () =>
        {
            if (OnLongPressContinued.HasDelegate)
            {
                await OnLongPressContinued.InvokeAsync();
            }
        });
    }

    private async void EndPress()
    {
        if (!_isPointerDown) return;

        var wasLongPress = _isLongPress;
        _isPointerDown = false;
        _isLongPress = false;

        // 清理计时器
        _longPressTimer?.Dispose();
        _longPressTimer = null;

        // 如果之前是长按状态，触发长按结束事件
        if (wasLongPress && OnLongPressEnd.HasDelegate)
        {
            await InvokeAsync(async () =>
            {
                await OnLongPressEnd.InvokeAsync();
            });
        }
    }

    public void Dispose()
    {
        _longPressTimer?.Dispose();
    }
}